﻿using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Client;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Model.DocumentDB
{
    public class DocumentViewModel : DocumentClientViewModelBase<CollectionViewModel>
    {
        private readonly Document _document;

        /// <summary>Constructor.</summary>
        /// <param name="client">Client API of the account.</param>
        /// <param name="document">Document object.</param>
        /// <param name="parent">Parent of the model.</param>
        internal DocumentViewModel(DocumentClient client, Document document, CollectionViewModel parent)
            : base(client, parent)
        {
            if (document == null)
            {
                throw new ArgumentNullException("document");
            }

            _document = document;
        }

        /// <summary>Returns a lazy-loading collection of documents of a given database collection.</summary>
        /// <param name="client">Client API of the account.</param>
        /// <param name="collectionsLink">Link to documents.</param>
        /// <param name="parent">Parent of the model.</param>
        /// <returns>Documents.</returns>
        internal static LoadableCollection<DocumentViewModel> GetLazyCollection(
            DocumentClient client,
            string documentsLink,
            CollectionViewModel parent)
        {
            if (client == null)
            {
                throw new ArgumentNullException("client");
            }
            if (string.IsNullOrWhiteSpace(documentsLink))
            {
                throw new ArgumentNullException("documentsLink");
            }

            return new LoadableCollection<DocumentViewModel>(async () =>
            {
                var documents = await client.ReadDocumentFeedAsync(documentsLink);
                var models = from document in documents
                             let model = new DocumentViewModel(client, document, parent)
                             //orderby model.Name
                             select model;

                return models;
            });
        }

        /// <summary>Document name.</summary>
        public string Name
        {
            get { return _document.Id; }
        }

        /// <summary>Document link.</summary>
        public string SelfLink
        {
            get { return _document.SelfLink; }
        }

        /// <summary>Returns the indented representation of the JSON content.</summary>
        public string Json
        {
            get
            {
                using (var stream = new MemoryStream())
                {
                    _document.SaveTo(stream, SerializationFormattingPolicy.Indented);

                    var buffer = stream.ToArray();
                    var text = ASCIIEncoding.ASCII.GetString(buffer);

                    return text;
                }
            }
        }

        /// <summary>Deletes the current document.</summary>
        /// <returns>Asynchronous deletion.</returns>
        public Task DeleteAsync()
        {
            var collection = Parent;

            collection.DocumentFolder.FolderContent.Remove(this);

            return Client.DeleteDocumentAsync(SelfLink);
        }
    }
}